//
//  MQNewFeatureViewController.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class MQNewFeatureViewController: UIViewController, UIScrollViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    private func setupUI(){
        view.addSubview(scrolleView)
        view.addSubview(pageControl)
        
        scrolleView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-100)
        }
        
        let count = 4
        for i in 0..<count{
            let pageImageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            pageImageView.frame = CGRectMake(CGFloat(i)*SCREENW, 0, SCREENW, SCREENH)
            
            scrolleView.addSubview(pageImageView)

            
            if i == count - 1 {
                setupLastPage(pageImageView)
            }
        }
        
        scrolleView.contentSize = CGSizeMake(CGFloat(count)*SCREENW, 0)
        pageControl.numberOfPages = count
    }
    // MARK: - 懒加载控件
    private lazy var scrolleView:UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = UIColor.purpleColor()
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        return scrollView
    }()
    
    private lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        return pageControl
    }()
    
    //进入微博的btn
    private lazy var enterBtn:UIButton = {
        
        let btn = UIButton()
        btn.addTarget(self, action: "didClickEnterBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("进入微博", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
    }()
    
    private lazy var shareBtn:UIButton = {
        
        let btn = UIButton()
        btn.addTarget(self, action: "didClickShareBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        btn.setTitle("进入微博", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,5,0,-5)
        
        return btn
    }()
    
    private func setupLastPage(pageImageView: UIImageView){
        pageImageView.userInteractionEnabled = true
        pageImageView.addSubview(enterBtn)
        pageImageView.addSubview(shareBtn)
        
        enterBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(pageImageView.snp_centerX)
            make.centerY.equalTo(pageImageView.snp_bottom).offset(-150)
        }
        
        shareBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(enterBtn)
            make.bottom.equalTo(enterBtn.snp_top).offset(-20)
        }
    }
    
// MARK: - 监听点击事件
    @objc private func didClickShareBtn(btn:UIButton){
        btn.selected = !btn.selected
    }
    @objc private func didClickEnterBtn(btn:UIButton){
        printfLog("新特性页切换界面")
        NSNotificationCenter.defaultCenter().postNotificationName(MQSwitchRootViewControllerNotification, object: nil)
    }
    
    // MARK: - UIScrollViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        pageControl.currentPage = NSInteger(page)
    }
}
